import React from 'react';
import {injectIntl} from './intl';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import {
    PAGE,
    TABLE,
    PAGINATION_OPTIONS_EXTEND,
    COMMON_TABLE_QUERY_FAIL_TEXT,
    COMMON_TABLE_EMPTY_TEXT,
    DATETIME_FORMAT
} from '../constants';
import {conventSorter} from './utils';
import {packingListType, packingType, packingListApplyStatus} from '../Enum';
import Ellipsis from 'ant-design-pro/lib/Ellipsis';
import {conventEnumValueToString, formatDateTime} from '../utils';

export class TablePanel extends React.PureComponent {
    state = {
        loading: false
    };
    onChangeSort = (pagination, filters, sorter) => {
        if(pagination.current - 1 === this.props.pageIndex && pagination.pageSize === this.props.pageSize)
            this.props.onChangeSort({
                ...conventSorter(sorter),
                pageIndex: PAGE.index
            });
    };
    render() {
        const columns = [
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.dealer',
                    defaultMessage: '总代'
                }),
                dataIndex: 'dealer',
                render: (text, record) => `(${record.dealerCode})${record.dealerName}`
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.code',
                    defaultMessage: '系统编号'
                }),
                sorter: true,
                dataIndex: 'code',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.pickAndPallet',
                    defaultMessage: '备料单及托盘号'
                }),
                dataIndex: 'pickAndPallet',
                render: text => <Ellipsis tooltip fullWidthRecognition length={32}>{text}</Ellipsis>
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.packingType',
                    defaultMessage: '包装类型'
                }),
                dataIndex: 'packingType',
                render: text => conventEnumValueToString(packingType, text)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.container',
                    defaultMessage: '包装规格'
                }),
                dataIndex: 'container',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.status',
                    defaultMessage: '状态'
                }),
                dataIndex: 'status',
                render: text => conventEnumValueToString(packingListType, text)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.applyStatus',
                    defaultMessage: '排柜申请状态'
                }),
                dataIndex: 'applyStatus',
                render: text => conventEnumValueToString(packingListApplyStatus, text)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.length',
                    defaultMessage: '长度'
                }),
                dataIndex: 'length',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.width',
                    defaultMessage: '宽度'
                }),
                dataIndex: 'width',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.height',
                    defaultMessage: '高度'
                }),
                dataIndex: 'height',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.volume',
                    defaultMessage: '体积'
                }),
                dataIndex: 'volume',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.netWeight',
                    defaultMessage: '净重'
                }),
                dataIndex: 'netWeight',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.grossWeight',
                    defaultMessage: '毛重'
                }),
                dataIndex: 'grossWeight',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.createTime',
                    defaultMessage: '导入时间'
                }),
                dataIndex: 'createTime',
                render: text => formatDateTime(text, DATETIME_FORMAT)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.remark',
                    defaultMessage: '备注'
                }),
                dataIndex: 'remark',
            },
        ];
        const pagination = {
            total: this.props.total,
            pageSize: this.props.pageSize,
            current: this.props.pageIndex + 1,
            onShowSizeChange: this.props.onPageSizeChange,
            onChange: this.props.onPageIndexChange,
            ...PAGINATION_OPTIONS_EXTEND
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={columns}
                    dataSource={this.props.data}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading}
                    onChange={this.onChangeSort}
                    locale={locale}
                    {...TABLE} />
            </div>
        );
    }
}
TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    errorMessage: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    onPageIndexChange: PropTypes.func.isRequired,
    onPageSizeChange: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {
    tableSearch,
} from './actions';
const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    errorMessage: state.getIn(['page', 'domainData', 'list', 'message']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    permissions: getPermissions(state)
});

const mapDispatchToProps = dispatch => ({
    onPageSizeChange: (pageIndex, pageSize) =>
        dispatch(
            tableSearch({
                pageIndex: PAGE.index,
                pageSize
            })
        ),
    onPageIndexChange: pageIndex =>
        dispatch(
            tableSearch({
                pageIndex: pageIndex - 1
            })
        ),
    onChangeSort: option => dispatch(tableSearch(option)),
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(TablePanel));
